#include "list.h"
#include <stdlib.h>

void list_init(list_t *list)
{
    list->size = 0;
}

void list_add(list_t *list, void *obj)
{
    node_t *node = (node_t *) malloc(sizeof(node_t));
    node->value = obj;
    if (list->head == NULL)
        list->head = node;
    else {
        node->next = list->head;
        list->head = node;
    }
    list->size++;
}

void *list_remove(list_t *list)
{
    node_t *res_node = list->head;
    void *res = res_node->value;

    list->head = list->head->next;
    list->size--;

    res_node->next = NULL;
    free(res_node);

    return res;
}

void *list_peek(list_t *list)
{
    return list->head->value;
}

void list_free(list_t *list)
{
    for (; list->head; list->head = list->head->next)
        list_remove(list);
    free(list);
}
